<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//分类管理
class Cate_model extends CI_Model{

        function __construct()
        {
                parent::__construct();
                $this->gift_table = 'gift_category';//商品分类
                $this->content_table = 'content_category';//内容分类
        }
        
        

        function gift_insert($data)
        {
                $this->db->insert($this->gift_table, $data);
                return $this->db->affected_rows();
        }

        public function GetGiftAll()
        {
                $this->db->select('*');
                $this->db->from($this->gift_table);
                $this->db->where('hide','0');
                $this->db->order_by('sort,id asc');
                $query = $this->db->get();
                return $query->result_array();
        }
        
        function gift_delete($delid)
        {	
                $this->db->where('id',$delid);
                $this->db->delete($this->gift_table);
                return $this->db->affected_rows();
        }
        function content_insert($data)
        {
                $this->db->insert($this->content_table, $data);
                return $this->db->affected_rows();
        }

        public function GetContentAll()
        {
                $this->db->select('*');
                $this->db->from($this->content_table);
                $this->db->where('hide','0');
                $this->db->order_by('sort,id asc');
                $query = $this->db->get();
                return $query->result_array();
        }
        
        function content_delete($delid)
        {	
                $this->db->where('id',$delid);
                $this->db->delete($this->content_table);
                return $this->db->affected_rows();
        }
        
        
        
        public function GetName($url)
        {
                $this->db->select('title');
                $this->db->from($this->table);
                $this->db->where('url',$url);
                $this->db->order_by('sort,id asc');
                $query = $this->db->get()->row_array();
                return $query['title'];
        }


        public function findkey(&$a,$id){
                foreach($a as $k=>$t){
                        if($t['url']==$id)return $k;
                        //print_r($t);die();
                        /*
                        foreach($t as $k1=>$t1){
                                if(in_array($id,$t1))
                                return $k;
                        }
                        */

                }

                return FALSE;
        }

        //递归生成树形数组
        public function tree($table,$pid='0') {
                $tree = array();
                foreach($table as $row){
                        if($row['pid']==$pid){
                                $tmp = $this->tree($table,$row['id']);
                                if($tmp){
                                        $row['children']=$tmp;
                                }else{
                                        $row['leaf'] = true;
                                }
                                $tree[]=$row;                
                        }
                }
                return $tree;        
        }

        public function getgiftlist($pid)
        {
                $this->db->select('*');
                $this->db->from($this->gift_table);
                $this->db->where('pid',$pid);
                $this->db->order_by('sort');
                //$this->db->limit($limit);
                $query = $this->db->get();
                return $query->result_array();
        }
        public function get_gift_list($where){
                $this->db->select('*');
                $this->db->from($this->gift_table);
                $this->db->where($where);
                $this->db->order_by('sort,id asc');
                //$this->db->limit($limit);
                $query = $this->db->get();
                return $query->result_array();
        }
        
        
        
        function get_gift_data($id){
                return $query = $this->db->where('id',$id)->get($this->gift_table)->row_array();
        }
        
        function gift_update($data,$id)
        {
                return $this->db->where('id',$id)->update($this->gift_table, $data);
        }
        
         public function getcontentlist($pid)
        {
                $this->db->select('*');
                $this->db->from($this->content_table);
                $this->db->where('pid',$pid);
                $this->db->order_by('sort');
                //$this->db->limit($limit);
                $query = $this->db->get();
                return $query->result_array();
        }
        public function get_content_list($where){
                $this->db->select('*');
                $this->db->from($this->content_table);
                $this->db->where($where);
                $this->db->order_by('sort,id asc');
                //$this->db->limit($limit);
                $query = $this->db->get();
                return $query->result_array();
        }

        function get_content_data($id){
                return $query = $this->db->where('id',$id)->get($this->content_table)->row_array();
        }
        
        function content_update($data,$id)
        {
                return $this->db->where('id',$id)->update($this->content_table, $data);
        }
        
        function get_list()
        {
                return $query = $this->db->get($this->table)->result_array();
        }

        function get_tree($parent_id = null)
        {
                $this->db->where('pid',$parent_id);
                $this->db->select('*');
                $query = $this->db->get($this->table);
                $list['son'] = $query->result_array();

                        foreach ($list['son'] as &$row) {
                                if(array_key_exists('last',$row))
                                {
                                        if(!$row['last'])
                                        {
                                        $temp = $this->get_tree($row['id']);//这里用了个递归
                                        $row['son'] = $temp['son'];
                                        }
                                }

                        }

                return $list;
        }
        function showbyselect($result,$layer=1){

                 //用来跟踪当前菜单的级数
                $root=$this->db->select('*')->where('pid','0')->get($this->table);
                return $this->_showbyselect($root,$layer);

        }



        function _showbyselect($result,$layer){  
                $rows=$result->num_rows();
                $menu=$result->result_array();
                $select="";    
                foreach($menu as $row){    
                  $channelid=trim($row['id']);
                  $select.="<option value='$channelid'>";
                  for($i=1;$i<$layer;$i++){
                         $select.="|——";
                  }
                  $select.=$row['title'];
                  $select.="</option>";  



                  $sql_sub = $this->db->select('*')->where('pid',$channelid)->get($this->table);

                  //$sql_sub="select * from ulife_menu where pid='$channelid'";
                  $result_sub=$this->db->query($sql_sub); //echo   $result_sub->num_rows() ;

                  if($result_sub->num_rows()>0){    
                        $layer++;      
                        $select.=$this->showbyselect($result_sub,$layer);        
                        $layer--;
                  }    
                }  
                return $select;
        }

}